<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>编辑教练员</title>
    <script src="../../res/js/jquery-3.4.1.min.js"></script>
    <script src="../../res/layui/layui.js"></script>
    <script src="../../res/js/Tools.js"></script>
    <link rel="stylesheet" href="../../res/layui/css/layui.css">
    <script src="../../res/js/base.js"></script>
    <script src="../../js/login_common.js"></script>
</head>
<body>
<script type="text/javascript">
    //全局同步
    $.ajaxSetup({
        async: false
    });

</script>
<script>
    var form = null;
    layui.use(['form', 'layedit', 'laydate'], function() {
        var  layer = layui.layer
            , layedit = layui.layedit
            , laydate = layui.laydate;

        form = layui.form;

        loadType();

        $("#creater").val(window.loginObj.user.userName);

        //日期
        laydate.render({
            elem: '#birthday'
        });
        laydate.render({
            elem: '#trainingYears'
        });
        laydate.render({
            elem: '#joinTime'
        });


        $("#closeSave").on("click",function () {
            //关闭当前层
            //当你在iframe页面关闭自身时
            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
            parent.layer.close(index); //再执行关闭
        });

        //点保存按钮
        $("#saveButton").on("click",function (obj) {

            var athleteName = $("#coachName").val();
            if(athleteName==""|| athleteName==null){
                layer.msg("请输入教练员名称");
                return;
            }

            var expertSpecia = $("#expertSpecia").val();
            if(expertSpecia==""|| expertSpecia==null){
                layer.msg("请输入选择专业");
                return;
            }

            var fromData = getFormData("fromone");//获取表单数据
            var url = "http://127.0.0.1:8900/emp/coach";
            $.ajax({
                url: url,//地址
                dataType: 'json',//数据类型
                type: 'POST',//类型
                timeout: 2000,//超时
                data:fromData,//数据
                //请求成功
                success: function (obj, status) {
                    if(obj.num==1){

                        layer.msg(obj.msg, {
                            icon: 1,
                            time: 500 //0.5秒关闭（如果不配置，默认是3秒）
                        }, function(){
                            //关闭当前层
                            //当你在iframe页面关闭自身时
                            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                            parent.layer.close(index); //再执行关闭
                            //刷新父页面
                            parent.newLoad();
                        });

                    }else{
                        layer.msg(obj.msg);
                    }
                },
                //失败/超时
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    if (textStatus === 'timeout') {
                        layer.msg("请求超时");
                        setTimeout(function () {
                            layer.msg('重新请求');
                        }, 2000);
                    }
                }
            });


            return false;
        });
    });

    function loadType(){

        //判断是不是查看
        var select = getQueryString("select");
        if(select==1) {

            $("#buttonDiv").css("display", "none");

            $(":text").prop("placeholder","");
            $("textarea").prop("placeholder","");

            var formq = document.forms[0];
            for (var i = 0; i < formq.length; i++) {
                var element = formq.elements[i];
                //部分元素可以编号     element.name  是元素自定义 name
                if (element.name != "auditEntity.auditContent"
                    && element.name != "auditEntity.auditAutograph"
                    && element.name != "auditEntity.auditTime"
                    && element.name != "auditEntity.auditState"
                    && element.name != "submitBtn"
                    && element.name != "reset"
                    && element.name != "id"
                    && element.name != "processInstanceId"
                    && element.name != "updateForm") {
                    element.disabled = "true";
                }
            }
        }

        url = getRootPath() + "AtsBaseSportController/findAll";
        data = null;
        $.post(url,data,function (obj) {
            $.each(obj,function (i,item) {
                $("#expertSpecia").append("<option value='"+item.sportId+"'>"+item.sportName+"</option>");
            });
            var id = getQueryString("id");
            if (id !="" && id!=null) {
                findByid(id);
            }
            form.render();
        },"json");
    };


    function findByid(id) {
        var url = "http://127.0.0.1:8900/emp/coach/"+id;

        $.ajax({
            url: url,//地址
            dataType: 'json',//数据类型
            type: 'GET',//类型
            timeout: 2000,//超时
            //请求成功
            success: function (obj, status) {
                $("#coachId").val(obj.coachId);
                $("#coachName").val(obj.coachName);
                $("#deptName").val(obj.deptName);
                $("#postName").val(obj.postName);
                $("#organName").val(obj.organName);
                $("#coachExp").val(obj.coachExp);
                $("#expertSpecia").val(obj.expertSpecia);
                $("#phone").val(obj.phone);
                $("#addr").val(obj.addr);
                $("#other").val(obj.other);

                form.render();
            },
            //失败/超时
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                if (textStatus === 'timeout') {
                    layer.msg("请求超时");
                    setTimeout(function () {
                        layer.msg('重新请求');
                    }, 2000);
                }
            }
        });
    }

</script>

<form class="layui-form" action="" id="fromone">

    <input id="coachId" name="coachId" type="hidden">
    <input id="creater" name="creater" type="hidden">

    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">教练员姓名</label>
            <div class="layui-input-block">
                <input type="text" name="coachName" id="coachName" lay-verify="title" autocomplete="off" placeholder="请输入教练员姓名" class="layui-input" maxlength="128">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">部门名称</label>
            <div class="layui-input-block">
                <input type="text" name="deptName" id="deptName" lay-verify="title" placeholder="请输入部门名称" autocomplete="off" class="layui-input" maxlength="128">
            </div>
        </div>

        <div class="layui-inline">
            <label class="layui-form-label">岗位名称</label>
            <div class="layui-input-block">
                <input type="text" name="postName" id="postName" lay-verify="title" autocomplete="off" placeholder="请输入岗位名称" class="layui-input" maxlength="128">
            </div>
        </div>

        <div class="layui-inline">
            <label class="layui-form-label">所在单位</label>
            <div class="layui-input-block">
                <input type="text" name="organName" id="organName" lay-verify="title" autocomplete="off" placeholder="请输入所在单位" class="layui-input" maxlength="128">
            </div>
        </div>

        <div class="layui-inline">
            <label class="layui-form-label">联系方式</label>
            <div class="layui-input-block">
                <input type="text" name="phone" id="phone" lay-verify="title" autocomplete="off" class="layui-input" placeholder="请输入联系方式" maxlength="128">
            </div>
        </div>

        <div class="layui-inline">
            <label class="layui-form-label">擅长</label>
            <div class="layui-input-block">
                <input type="text" name="coachExp" id="coachExp" lay-verify="title" autocomplete="off" class="layui-input" placeholder="请输入擅长" maxlength="128">
            </div>
        </div>

        <div class="layui-inline">
            <label class="layui-form-label">专业</label>
            <div class="layui-input-block">
                <select name="expertSpecia" id="expertSpecia" lay-filter="aihao">
                    <option value=""></option>
                </select>
            </div>
        </div>
    </div>

    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">通讯地址</label>
        <div class="layui-input-block">
            <textarea placeholder="请输入内容" class="layui-textarea" name="addr" id="addr" maxlength="128"></textarea>
        </div>
    </div>

    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">其他说明</label>
        <div class="layui-input-block">
            <textarea placeholder="请输入内容" class="layui-textarea" name="other" id="other" maxlength="128"></textarea>
        </div>
    </div>

    <div class="layui-form-item" id="buttonDiv">
        <div class="layui-input-block">
            <input type="button" value="保存" class="layui-btn" id="saveButton">
            <input type="button" value="取消" class="layui-btn layui-btn-primary" id="closeSave">
        </div>
    </div>

</form>
</body>
</html>